//
//  HomeBaseDetailViewController.m
//  GiftNote
//
//  Created by lanou on 15/8/12.
//  Copyright (c) 2015年 GN. All rights reserved.
//

#import "HomeBaseDetailViewController.h"
#import "HomeHeader.h"
#import "HomeDetailHeadView.h"
#import "LORequestManger.h"
#import "HomeDetailModel.h"
#import "HomeDetailBuyViewController.h"
#import "MBProgressHUD+MJ.h"

@interface HomeBaseDetailViewController ()<UIWebViewDelegate, UIScrollViewDelegate>

@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)UIWebView *webView;

@property (nonatomic, strong)HomeDetailHeadView *headView;
@property (nonatomic, strong)HomeDetailBuyViewController *buyVC;

@end

@implementation HomeBaseDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatScrollView];
    [self readData];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"攻略详情";
    //  改变标题的颜色
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                [UIColor whiteColor],
                                NSForegroundColorAttributeName, nil];
    [self.navigationController.navigationBar setTitleTextAttributes:attributes];
    
    [MBProgressHUD showMessage:@"正在加载中...."];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated
{
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
}


#pragma mark - 创建scrollView

- (void)creatScrollView
{
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kHomeWidth, kHomeHeight)];
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    
    _headView = [[HomeDetailHeadView alloc]initWithFrame:CGRectMake(0, 0, kHomeWidth, 318 * (kHomeWidth + 128) / 700)];
    [_scrollView addSubview:_headView];
    
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(_headView.frame) + 15, kHomeWidth - 20, 0)];
    _webView.delegate = self;
    [_scrollView addSubview:_webView];
    
    
    
}




#pragma mark - 数据解析

- (void)readData
{
    NSString *url = [kHomeDetail stringByAppendingString:_ID];
    [LORequestManger GET:url success:^(id response) {
        
        NSDictionary *dic = (NSDictionary *)response;
        NSDictionary *newDic = dic[@"data"];
        HomeDetailModel *detailModel = [[HomeDetailModel alloc]init];
        [detailModel setValuesForKeysWithDictionary:newDic];
        _headView.detailModel = detailModel;
        

        
        NSRange range1 = [detailModel.content_html rangeOfString:@"<script type=\"text/javascript\">var"];
        NSRange range2 = [detailModel.content_html rangeOfString:@"</script>\n</body>"];
        
        NSRange newRange = NSMakeRange(range1.location, range2.location - range1.location);
        NSString *replacedStr = [detailModel.content_html substringWithRange:newRange];
        
        detailModel.content_html = [detailModel.content_html stringByReplacingOccurrencesOfString:replacedStr withString:@""];
        
        
        [_webView loadHTMLString:detailModel.content_html baseURL:nil];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error:%@",error);
    }];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
    _webView.frame = CGRectMake(0, CGRectGetMaxY(_headView.frame) + 15, kHomeWidth, height);
    _scrollView.contentSize = CGSizeMake(kHomeWidth, height + _headView.frame.size.height + 25 + kHomeHeight / 5);


    
    [MBProgressHUD hideHUD];

}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (![request.URL.relativeString isEqualToString:@"about:blank"] ) {
        
        _buyVC = [[HomeDetailBuyViewController alloc]init];
        [self.navigationController pushViewController:_buyVC animated:YES];
        NSString *string = [request.URL.relativeString substringFromIndex:30];
        _buyVC.ID = string;
        
        return NO;
    }
    return YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat size = scrollView.contentOffset.y;
    
    if (size < -64) {
        _headView.imageView.frame = CGRectMake(size, size + 64, kHomeWidth - 2 * size, 318 * (kHomeWidth - 2 * size) / 700 - (size + 64));
    } else if (size > -64) {
        
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
